﻿/***************************************************************************

Copyright (c) 2008 Microsoft Corporation. All rights reserved.

***************************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Shell.Interop;

namespace VsxFactory.Modeling.VisualStudio
{
    public interface IOutputWindow
    {
        void Clear();
        void Write(string message);
        void Write(string format, params object[] args);
    }
    /// <summary>
    /// Class to interact with the VS output window
    /// </summary>
    public class VsOutputWindow : IOutputWindow
    {
        #region Fields
        IVsOutputWindowPane pane;
        #endregion

        #region Constructors
        /// <summary>
        /// Initializes a new instance of the <see cref="OutputWindow"/> class.
        /// </summary>
        /// <param name="serviceProvider">The service provider.</param>
        public VsOutputWindow(IServiceProvider serviceProvider, Guid paneGuid, string name)
        {
            CreatePane(serviceProvider, paneGuid, name);
        }
        #endregion

        #region Public Implementation
        /// <summary>
        /// Creates the pane.
        /// </summary>
        /// <param name="paneName">Name of the pane.</param>
        /// <param name="visible">if set to <c>true</c> [visible].</param>
        /// <param name="clearWithSolution">if set to <c>true</c> [clear with solution].</param>
        /// <returns></returns>
        private void CreatePane(IServiceProvider serviceProvider, Guid guidPane, string paneName)
        {
            if (string.IsNullOrEmpty(paneName))
                throw new ArgumentNullException("paneName");

            var service = serviceProvider.GetService(typeof(SVsOutputWindow)) as IVsOutputWindow;

            if (ErrorHandler.Failed(service.GetPane(guidPane, out pane)) && (pane == null))
            {
                if (ErrorHandler.Succeeded(service.CreatePane(ref guidPane, paneName, 1, 1)))
                {
                    service.GetPane(ref guidPane, out pane);
                }
            }

            if (pane == null)
            {
                throw new InvalidOperationException();
            }

            pane.Activate();
        }

        /// <summary>
        /// Writes the specified format.
        /// </summary>
        /// <param name="format">The format.</param>
        /// <param name="args">The args.</param>
        public void Write(string format, params object[] args)
        {
            Guard.ArgumentNotNullOrEmptyString(format, "format");
            Write(String.Format(format, args));
        }

        /// <summary>
        /// Writes the message.
        /// </summary>
        /// <param name="message">The message.</param>
        public void Write(string message)
        {
            if (!string.IsNullOrEmpty(message))
            {
#if DEBUG
                message = String.Format("{0} - {1}", DateTime.Now.ToLongTimeString(), message);
#endif
                message = FormatMessage(message);

                if (pane != null)
                {
                    pane.OutputString(message);
                }
            }
        }

        /// <summary>
        /// Clears all messages
        /// </summary>
        public void Clear()
        {
            pane.Clear();
        }
        #endregion

        #region Private Implementation
        /// <summary>
        /// Formats the message.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <returns></returns>
        private static string FormatMessage(string message)
        {
            string formattedMessage = message;

            if (!formattedMessage.EndsWith("\r\n", StringComparison.OrdinalIgnoreCase))
            {
                formattedMessage = string.Concat(formattedMessage, "\r\n");
            }

            return formattedMessage;
        }
        #endregion
    }
}